package cc.edge2.sns.services;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import net.arnx.jsonic.JSON;
import cc.edge2.sns.commons.PMF;
import cc.edge2.sns.models.FriendRelation;
import cc.edge2.sns.models.SNSUser;

public class ReleaseFriendService {
	public String execute(Map params, SNSUser loginUser) {
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		long currentUserId = Integer.parseInt((String)params.get("userId"));
		
		try {
			Query query = pm.newQuery(FriendRelation.class);
			try {
				query.setFilter("userId1 == :userId1Param && userId2 == :userId2Param");
				List<FriendRelation> list = (List<FriendRelation>)query.execute(loginUser.getUserId(), currentUserId);
				for(FriendRelation friendRelation : list) {
					pm.deletePersistent(friendRelation);
				}
			} finally {
				query.closeAll();
			}
			query = pm.newQuery(FriendRelation.class);
			try {
				query.setFilter("userId1 == :userId1Param && userId2 == :userId2Param");
				List<FriendRelation> list = (List<FriendRelation>)query.execute(currentUserId, loginUser.getUserId());
				for(FriendRelation friendRelation : list) {
					pm.deletePersistent(friendRelation);
				}
			} finally {
				query.closeAll();
			}
		} finally {
			pm.close();
		}
				
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> contents = new HashMap<String, Object>();
		Map<String, Object> status = new HashMap<String, Object>();
		status.put("code", "0");
		status.put("message", "success");
		result.put("status", status);
		String value = JSON.encode(result);
		return value;
	}
}
